//
//  NSDate+AYExtension.m
//  CreateTools
//
//  Created by AY on 16/6/27.
//  Copyright © 2016年 Jasperay. All rights reserved.
//

#import "NSDate+AYExtension.h"

@implementation NSDate (AYExtension)

+ (BOOL)isThisYearWithTarget:(NSDate *)target{
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy";
    
    NSString *currentYear = [formatter stringFromDate:currentDate];
    NSString *targetYear = [formatter stringFromDate:target];
    return [currentYear isEqualToString:targetYear];
}


+ (BOOL)isTodayWithTarget:(NSDate *)target{
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    
    NSString *currentResult = [formatter stringFromDate:currentDate];
    NSString *targetResult = [formatter stringFromDate:target];
    
    return [currentResult isEqualToString:targetResult];
}

+ (BOOL)isYesterdayWithTarget:(NSDate *)target{
    
    //取到当前时间
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *createStr = [formatter stringFromDate:target];
    NSString *currentStr = [formatter stringFromDate:currentDate];
    
    NSDate *createD = [formatter dateFromString:createStr];
    NSDate *currentD = [formatter dateFromString:currentStr];
    
    NSCalendar *canlendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [canlendar components:NSCalendarUnitDay fromDate:createD toDate:currentD options:0];
    
    return components.day == 1;
}

+ (BOOL)isTheDayBeforeYesterdayWithTarget:(NSDate *)target
{
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    
    NSString *createStr = [formatter stringFromDate:target];
    NSString *currentStr = [formatter stringFromDate:currentDate];
    
    NSDate *createD = [formatter dateFromString:createStr];
    NSDate *currentD = [formatter dateFromString:currentStr];
    
    
    NSCalendar *canlendar = [NSCalendar currentCalendar];
    
    NSDateComponents *components = [canlendar components:NSCalendarUnitDay fromDate:createD toDate:currentD options:0];
    
    NSLog(@"%zd",components.day);
    return components.day == 2;
}

- (NSInteger)getNowWeekday {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDate *now = [NSDate date];
    // 话说在真机上需要设置区域，才能正确获取本地日期，天朝代码:zh_CN
    calendar.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    comps = [calendar components:unitFlags fromDate:now];
    return [comps day];
}


@end
